﻿//////////////////////////////////////////////////////////////////////////////
//
// Copyright © 1998-2024 Glodon Company Limited.
//
// Licensed under the MIT License
//
// Permission is hereby granted, free of charge, to any person obtaining a
// copy of this software and associated documentation files (the “Software”),
// to deal in the Software without restriction, including without limitation
// the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
// sell copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
// THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
//
//////////////////////////////////////////////////////////////////////////////
// qt headers
#include <QVBoxLayout>
#include <QSpacerItem>
#include <QLabel>
#include <QVariant>

// custom headers
#include "DrawerChest.h"
#include "CustomDrawer.h"
#include "DebugMessage.h"
#include "GbmpNew.h"
#include "EnableCompileWarning_The_LAST_IncludeInCpp.h" 

namespace gmepgcs
{
    DrawerChest::DrawerChest(QWidget *parent) :
        QWidget(parent),
        m_pChestLayout(nullptr),
        m_pSpacerLabel(nullptr)
    {
        InitUI();
    }

    DrawerChest::~DrawerChest()
    {
    }

    void DrawerChest::AddDrawer(CustomDrawer * drawer)
    {
        if (!drawer)
        {
            return;
        }
        
        connect(drawer, &CustomDrawer::Drawn, [=](bool drawnIn, int impactedHeight) {
            if (drawnIn)
            {
                setFixedHeight(height() - impactedHeight);
            }
            else
            {
                setFixedHeight(childrenRect().height());
                //adjustSize();
            }
        });
        DBG_WARN_AND_RETURN_VOID_UNLESS(m_pChestLayout, L"m_pChestLayout为空", L"GMEP", L"2024-01-30");
        m_pChestLayout->addWidget(drawer);
        m_pChestLayout->setAlignment(drawer, Qt::AlignTop);
    }


    void DrawerChest::InitUI()
    {
        // main layout
        QHBoxLayout *pMainLayout = NEW_AS_QT_CHILD(QHBoxLayout, this);
        DBG_WARN_AND_RETURN_VOID_UNLESS(pMainLayout, L"pMainLayout为空", L"GMEP", L"2024-01-30");
        pMainLayout->setContentsMargins(0, 0, 0, 0);
        pMainLayout->setSpacing(0);
        setLayout(pMainLayout);
        pMainLayout->setSizeConstraint(QLayout::SizeConstraint::SetMinimumSize);

        QFrame *pMainFrame = NEW_AS_QT_CHILD(QFrame,this);
        DBG_WARN_AND_RETURN_VOID_UNLESS(pMainFrame, L"pMainFrame为空", L"GMEP", L"2024-01-30");
        pMainLayout->addWidget(pMainFrame);
        pMainFrame->setObjectName("DrawerChestMainFrame");
        pMainFrame->setStyleSheet("QFrame#DrawerChestMainFrame{background:transparent;}");

        // chest layout
        m_pChestLayout = NEW_AS_QT_CHILD(QVBoxLayout, pMainFrame);
        DBG_WARN_AND_RETURN_VOID_UNLESS(m_pChestLayout, L"m_pChestLayout为空", L"GMEP", L"2024-01-30");
        pMainFrame->setLayout(m_pChestLayout);
        m_pChestLayout->setContentsMargins(5, 5, 5, 5);
        m_pChestLayout->setSpacing(5);
        m_pChestLayout->setSizeConstraint(QLayout::SizeConstraint::SetMinimumSize);
        m_pChestLayout->setAlignment(Qt::AlignTop);

    }

}